package api

import (
	"autoclave/model"
	"github.com/gin-gonic/gin"
)

type HomeController struct {
}

func (con HomeController) GetRealTimeState(ctx *gin.Context) {

	data := model.Equip{}
	err := model.DB.Where("id = 1").Find(&data).Error
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    1,
			"message": "sql error!",
			"data":    err.Error(),
		})
		return
	}
	if data.OnState == 1 {
		data.OnOff = 1
	} else {
		data.OnOff = 0
	}
	if data.SlackState == 1 {
		data.SlackTight = 1
	} else {
		data.SlackTight = 0
	}
	ctx.JSON(200, gin.H{
		"code":    0,
		"message": "success",
		"data":    data,
	})
}

func (con HomeController) GetRuntimeData(ctx *gin.Context) {
	data := model.PointFull{}
	err := model.DB.Order("id desc").Limit(1).Find(&data).Error
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    1,
			"message": "sql error!",
			"data":    err.Error(),
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code":    0,
		"message": "success",
		"data":    data,
	})

}

func (con HomeController) GetLogList(ctx *gin.Context) {
	//param := struct {
	//	Page int `json:"page"`
	//	Size int `json:"size"`
	//}{}
	data := make([]model.Point, 0)
	err := model.DB.Order("id desc").Limit(50).Find(&data).Error
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    1,
			"message": "sql error!",
			"data":    err.Error(),
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code":    0,
		"message": "success",
		"data":    data,
	})

}
